package cn.itcast.ssm.controller;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import cn.itcast.ssm.po.ItemsCustom;
import cn.itcast.ssm.po.ItemsQueryVo;
import cn.itcast.ssm.service.ItemsService;

/**
* Created By lijiliang on 2018年7月11日 下午1:30:34
*/

@Controller
@RequestMapping("/items")
public class ItemsController {

	@Autowired 
	ItemsService itemsService;
	
	//商品分类
	//itemstypes表示最终将方法返回值放到request中的key
	@ModelAttribute("itemtypes")
	public Map<String ,String> getItemTypes(){
		Map<String ,String> itemTypes = new HashMap<String,String>();
		itemTypes.put("101", "数码");
		itemTypes.put("102", "母婴");
		return itemTypes;
	}
	
	//查询商品
	@RequestMapping("/queryItems")
	public ModelAndView queryItems(ItemsQueryVo itemsQueryVo )throws Exception{
		List<ItemsCustom> itemsList=itemsService.findItemsList(itemsQueryVo);
		
		ModelAndView mv=new ModelAndView();
		mv.addObject("itemsList", itemsList);
		mv.setViewName("items/itemsList");
		
		return mv;
	}
	//修改商品信息
	/*@RequestMapping("editItems")
	public ModelAndView editItems()throws Exception{
		ItemsCustom itemsCustom=itemsService.findItemsById(1);
		
		ModelAndView mv=new ModelAndView();
		mv.addObject("itemsCustom", itemsCustom);
		mv.setViewName("items/editItems");
		return mv;
	}*/
	
	@RequestMapping("editItems")
	public String editItems(Model model,Integer id)throws Exception{
		ItemsCustom itemsCustom=itemsService.findItemsById(id);
		
		/*ModelAndView mv=new ModelAndView();
		mv.addObject("itemsCustom", itemsCustom);
		mv.setViewName("items/editItems");
*/		
		
		model.addAttribute("itemsCustom", itemsCustom);
		return "items/editItems";

	}
	
	//商品信息修改提交
	/*@RequestMapping("editItemsSubmit")
	public ModelAndView editItemsSubmit()throws Exception{
		ModelAndView mv=new ModelAndView();
		mv.setViewName("success");
		return mv;
	}*/
	
	@RequestMapping("/editItemsSubmit")
	public String editItemsSubmit(Model model,Integer id,@Validated ItemsCustom itemsCustom 
			,BindingResult bindingResult
			,MultipartFile items_pic)throws Exception{
		if(bindingResult.hasErrors()) {
			List<ObjectError> allErrors=bindingResult.getAllErrors();
			for(ObjectError objectError : allErrors) {
				System.out.println(objectError.getDefaultMessage());
			}
			model.addAttribute("allErrors", allErrors);
			return "items/editItems";
		}
		//原始名称
		String originalFilename = items_pic.getOriginalFilename();
		//上传图片
		if(items_pic!=null && originalFilename!=null && originalFilename.length()>0) {
			//存储图片的物理路径
			String pic_path="F:\\develop\\upload\\temp\\";
			
			//新的图片名称
			String newfilename= UUID.randomUUID()+originalFilename.substring(originalFilename.lastIndexOf("."));
			File newFile = new File(pic_path+newfilename);
			//将内存中的数据写入磁盘
			items_pic.transferTo(newFile);
			itemsCustom.setPic(newfilename);
		}
		
		itemsService.updateItems(id, itemsCustom);
		return "redirect:queryItems.action";
	}
	
	@RequestMapping("/deleteItems")
	public String deleteItems(Integer[] items_id) throws Exception{
		
		return "success";
	}
	
	//批量修改商品的页面（将商品信息查询出来，在页面可以编辑商品信息）
	@RequestMapping("/editItemsQuery")
	public ModelAndView editItemsQuery(ItemsQueryVo itemsQueryVo )throws Exception{
		List<ItemsCustom> itemsList=itemsService.findItemsList(itemsQueryVo);
		
		ModelAndView mv=new ModelAndView();
		mv.addObject("itemsList", itemsList);
		mv.setViewName("items/editItemsQuery");
		
		return mv;
	}
	
	//批量修改商品的提交
	@RequestMapping("/editItemsAllSubmit")
	public String editItemsAllSubmit(ItemsQueryVo itemsQueryVo) throws Exception{
		
		return "success";
	}
}
